﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DTO;
namespace DAO
{
    public  class HoaDonBanHangDAO
    {
        public string KetNoi()
        {
            string kq = "";
            SqlConnection conn = DataProvider.ConnectDB();
            if (conn == null)
                kq = "THat bai";
            else
                kq = "Thanh cong";
            conn.Close();
            return kq;

        }
        
        public List<HoaDonBanHangDTO> LayDanhSachHoaDon()
        {
            List<HoaDonBanHangDTO> danhsach = new List<HoaDonBanHangDTO>();
            string sql = "Select * From HOADON where LoaiHoaDon=0";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                HoaDonBanHangDTO hdDTO = new HoaDonBanHangDTO();
                hdDTO.MaHoaDon = reader.GetInt32(0);
                hdDTO.MaKhachHang = reader.GetInt32(1);
                hdDTO.NgayLap = reader.GetDateTime(2);
                hdDTO.Vat = reader.GetString(3);
                hdDTO.NgayGiao = reader.GetDateTime(4);
                hdDTO.TenNguoiNhan = reader.GetString(5);
                hdDTO.SdtNguoiNhan = reader.GetString(6);
                hdDTO.MaSoThue = reader.GetString(7);
                hdDTO.GiaoDen = reader.GetString(8);
                hdDTO.TraTruoc = reader.GetDouble(9);
                hdDTO.ConNo = reader.GetDouble(10);
                hdDTO.PhiGiaoHang = reader.GetDouble(11);
                hdDTO.ThanhTien = reader.GetDouble(12);
                hdDTO.TinhTrang = reader.GetInt32(13);
                hdDTO.LoaiHoaDon = reader.GetInt32(14);
                danhsach.Add(hdDTO);
            }
            conn.Close();
            return danhsach;
        }
        public string ThemHoaDonBanHang(HoaDonBanHangDTO hdDTO)
        {
            string mess = "";
            
            string sql = "Insert into";
            sql+= " HOADON(MaHoaDon, MaKhachHang, NgayLap, VAT, NgayGiao, TenNguoiNhan, SDTNguoiNhan, MaSoThue,GiaoDen, TraTruoc, ConNo, PhiGiaoHang, ThanhTien, TinhTrang, LoaiHoaDon)";
            sql += " values(@MaHoaDon, @MaKhachHang, @NgayLap, @VAT, @NgayGiao, @TenNguoiNhan, @SDTNguoiNhan, @MaSoThue, @GiaoDen, @TraTruoc, @ConNo, @PhiGiaoHang, @ThanhTien, @TinhTrang, @LoaiHoaDon)";
            try
            {
                SqlConnection conn = DataProvider.ConnectDB();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter para;
                para = cmd.Parameters.Add("@MaHoaDon", SqlDbType.Int);
                para.Value = hdDTO.MaHoaDon;
                para = cmd.Parameters.Add("@MaKhachHang", SqlDbType.Int);
                para.Value = hdDTO.MaKhachHang;
                para = cmd.Parameters.Add("@NgayLap", SqlDbType.DateTime);
                para.Value = hdDTO.NgayLap;
                para = cmd.Parameters.Add("@VAT", SqlDbType.NChar);
                para.Value = hdDTO.Vat;
                para = cmd.Parameters.Add("@NgayGiao", SqlDbType.DateTime);
                para.Value = hdDTO.NgayGiao;
                para = cmd.Parameters.Add("@TenNguoiNhan", SqlDbType.NVarChar);
                para.Value = hdDTO.TenNguoiNhan;
                para = cmd.Parameters.Add("@SDTNguoiNhan", SqlDbType.VarChar);
                para.Value = hdDTO.SdtNguoiNhan;
                para = cmd.Parameters.Add("@MaSoThue", SqlDbType.NChar);
                para.Value = hdDTO.MaSoThue;
                para = cmd.Parameters.Add("@GiaoDen", SqlDbType.NVarChar);
                para.Value = hdDTO.GiaoDen;
                para = cmd.Parameters.Add("@TraTruoc", SqlDbType.Float);
                para.Value = hdDTO.TraTruoc;
                para = cmd.Parameters.Add("@ConNo", SqlDbType.Float);
                para.Value = hdDTO.ConNo;
                para = cmd.Parameters.Add("@PhiGiaoHang", SqlDbType.Float);
                para.Value = hdDTO.PhiGiaoHang;
                para = cmd.Parameters.Add("@ThanhTien", SqlDbType.Float);
                para.Value = hdDTO.ThanhTien;
                para = cmd.Parameters.Add("@TinhTrang", SqlDbType.Int);
                para.Value = hdDTO.TinhTrang;
                para = cmd.Parameters.Add("@LoaiHoaDon", SqlDbType.Int);
                para.Value = hdDTO.LoaiHoaDon;
                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            return mess;
        }
        public int LayMaHoaDonBanHang()
        {
            int max = 0;
            List<HoaDonBanHangDTO> listHD = LayDanhSachHoaDon();
            if (listHD.Count < 1)
                return max;
            for (int i = 0; i < listHD.Count; i++)
            {
                if (listHD[i].MaHoaDon > max)
                    max = listHD[i].MaHoaDon;
            }
            return max;
        }
    }
}
